<html>
<head>
  <title>serialize.py</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
An example of how to send and receive arbitrary python objects, such as dictionaries.
&quot;</font><font color="#008000">&quot;&quot;</font>

<strong>import</strong> <font color="#2040a0">pickle</font>
<strong>import</strong> <font color="#2040a0">mpi</font>

<font color="#2040a0">somedata</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#008000">&quot;hello&quot;</font>,<font color="#008000">&quot;world&quot;</font>,<font color="#008000">&quot;!&quot;</font><font color="4444FF">]</font>
<font color="#2040a0">somedict</font> <font color="4444FF">=</font> <font color="4444FF"><strong>{</strong></font><font color="4444FF"><strong>}</strong></font>

<font color="#2040a0">i</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
<strong>for</strong> <font color="#2040a0">item</font> <strong>in</strong> <font color="#2040a0">somedata</font><font color="4444FF">:</font>
    <font color="#2040a0">somedict</font><font color="4444FF">[</font><font color="#2040a0">i</font><font color="4444FF">]</font> <font color="4444FF">=</font> <font color="#2040a0">item</font>
    <font color="#2040a0">i</font> <font color="4444FF">+</font><font color="4444FF">=</font> <font color="#FF0000">1</font>


<font color="#2040a0">def</font> <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font><font color="4444FF">:</font>
    <font color="#2040a0">rank</font>,<font color="#2040a0">size</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">init</font><font color="4444FF">(</font><font color="4444FF">)</font>
    
    <font color="#2040a0">serial_dict</font> <font color="4444FF">=</font> <font color="#2040a0">pickle</font>.<font color="#2040a0">dumps</font><font color="4444FF">(</font><font color="#2040a0">somedict</font><font color="4444FF">)</font>

    <font color="#2040a0">mpi</font>.<font color="#2040a0">isend</font><font color="4444FF">(</font> <font color="#2040a0">serial_dict</font>, <font color="#2040a0">len</font><font color="4444FF">(</font><font color="#2040a0">serial_dict</font><font color="4444FF">)</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_CHAR</font>, <font color="#FF0000">0</font>, <font color="#FF0000">0</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_COMM_WORLD</font> <font color="4444FF">)</font>

    <font color="#2040a0">new_serial_dict</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">recv</font><font color="4444FF">(</font> <font color="#2040a0">len</font><font color="4444FF">(</font> <font color="#2040a0">serial_dict</font><font color="4444FF">)</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_CHAR</font>, <font color="#FF0000">0</font>, <font color="#FF0000">0</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_COMM_WORLD</font> <font color="4444FF">)</font>
    <strong>print</strong> <font color="#2040a0">new_serial_dict</font>

    <font color="#2040a0">mpi</font>.<font color="#2040a0">finalize</font><font color="4444FF">(</font><font color="4444FF">)</font>

    <font color="#2040a0">newdict</font> <font color="4444FF">=</font> <font color="#2040a0">pickle</font>.<font color="#2040a0">loads</font><font color="4444FF">(</font> <font color="#2040a0">new_serial_dict</font> <font color="4444FF">)</font>
    <strong>print</strong> <font color="#2040a0">newdict</font>
    <strong>return</strong>

<strong>if</strong> <font color="#2040a0">__name__</font><font color="4444FF">=</font><font color="4444FF">=</font><font color="#008000">&quot;__main__&quot;</font><font color="4444FF">:</font>
    <font color="#2040a0">main</font><font color="4444FF">(</font><font color="4444FF">)</font>

</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
